<?php
/********************************************************
 *
 * 此文件的作用:
 *		管理员操作
 *
 * @copyright Copyright (c) 2008-2009
 * @author 刘昌功 lcg635@126.com
 *
 ********************************************************/
defined('APP_DIR') or die('404 Not Found');

FLEA::loadClass('Common_Model');

class Model_Admin extends Common_Model {
	function __construct() {
		parent::__construct();
		$this->_tb = FLEA::getSingleton('Table_Admin');
	}
	
	function login($data, $imgcode = false) {
		$rbac = FLEA::getSingleton('FLEA_RBAC');
		if ( null != $rbac->getUser() )
			return '您已经登录';
		if ( true == $imgcode ) {
			$checkImgcode = $this->checkImgcode($data['imgcode']);
			if ( null != $checkImgcode )
				return $checkImgcode;
		}
		$user = $this->_tb->validateUser($data['username'], $data['password'], true);
		if( false == $user )
			return '用户名或密码错误';
		$sessionuser = array(
			'userid' => $user['admin_id'],
			'username' => $user['username'],
		);
		$rbac->setUser($sessionuser);
		return null;
	}
	
	function getInfo($aid) {
		$this->_tb->enableLink('roles');
		$result = $this->_tb->find('admin_id='.$aid);
		return $result;
	}

	function getList($page = 0, $pageSize = 15) {
		
		FLEA::loadHelper('pager');
		$pager = new FLEA_Helper_Pager($this->_tb, $page, $pageSize, null, 'created desc');
		$lists = $pager->findAll();

		foreach ($lists as $k => $v)
			$lists[$k]['level'] = 1 == $v['level'] ? '超级管理员' : '普通管理员';

		$viewData = array(
			'lists' => $lists,
			'pagerData' => $pager->getPagerData()
		);
		return $viewData;
	}

	function insert($data) {
		if( $this->_tb->existsUsername($data['username']) )
			$result[] = '该用户名已经存在!';
        if ( $data['confirmPassword'] != $data['password'] )
            $result[] = '两次输入的密码不一致!';
		if ( count($result) > 0 )
			return $result;
		$this->_tb->create($data);
		return null;
	}

	function delete($uid) {
		return $this->_tb->removeByPkv($uid);
	}

	function getForm($type = null, $data = null) {
		return $this->_tb->form($type, $data);
	}

	function update($row) {
		$resutl = $this->_tb->update($row);
		return $result;
	}
}
